package com.jerry.huihome.module.find.fragment;

import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseFragment;
import com.jerry.huihome.constant.Urls;
import com.jerry.huihome.module.find.adapter.DesignerAdapter;
import com.jerry.huihome.module.find.entity.Designer;
import com.jerry.huihome.utils.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/5/10.
 */
public class DesignerDetailFragment extends BaseFragment {
    @BindView(R.id.prf_find_select)
    PullToRefreshListView prfSelect;
    private int pageIndex = 1;
    private String url;
    private List<Designer.BodyBean.DesignersBean> list = new ArrayList<>();
    private DesignerAdapter adapter;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_designer_detail;
    }

    @Override
    protected void initViews() {
        adapter = new DesignerAdapter(getContext(),list);
        prfSelect.getRefreshableView().setDivider(null);
        prfSelect.getRefreshableView().setSelector(android.R.color.transparent);
        prfSelect.setMode(PullToRefreshBase.Mode.BOTH);
        prfSelect.setAdapter(adapter);
        prfSelect.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageIndex = 1;
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageIndex++;
                loadData();
            }
        });

    }

    @Override
    protected void loadData() {
        url = Urls.FIND_DESIGNER_URL+"pageIndex="+pageIndex;
        OkHttpUtils.get(url, new OkHttpUtils.OnGetDataListener() {
            @Override
            public void onResponse(String url, String data) {
                if (data != null){
                    if (pageIndex == 1){
                        adapter.getList().clear();
                    }
                    Gson gson = new Gson();
                    Designer designer = gson.fromJson(data, Designer.class);
                    list.addAll(designer.getBody().getDesigners());
                    adapter.notifyDataSetChanged();
                    prfSelect.onRefreshComplete();
                }
            }

            @Override
            public void onFailure(String url, String error) {

            }
        });
    }
}
